package com.xinxin.blockchain.api.controller;

import com.github.pagehelper.PageInfo;
import com.xinxin.base.controller.BaseController;
import com.xinxin.base.entity.ApiResult;
import com.xinxin.base.entity.PageRequest;
import com.xinxin.base.util.HttpClientUtil;
import com.xinxin.base.util.JsonUtil;
import com.xinxin.blockchain.asset.enums.AssetBusinessTypeEnum;
import com.xinxin.blockchain.asset.enums.AssetTypeEnum;
import com.xinxin.blockchain.constant.ErrorCode;
import com.xinxin.blockchain.model.AccountAsset;
import com.xinxin.blockchain.model.Member;
import com.xinxin.blockchain.service.IAccountAssetService;
import com.xinxin.blockchain.service.IMemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.Map;

/**
 * @author ys
 * @date 2021/5/19
 */
@Api(tags = "测试")
@Validated
@RestController
@RequestMapping("test")
public class TestController extends BaseController {

    @Autowired
    private IMemberService memberService;
    @Autowired
    private IAccountAssetService accountAssetService;

    @ApiOperation("汇率接口")
    @GetMapping("rate/{currency}")
    public ApiResult<Map> rate(
            @ApiParam(value = "货币 XCH/USD", required = true)
            @NotBlank @PathVariable String currency,
            @ApiParam("TLS")
            @RequestParam(defaultValue = "false") boolean https) {
        String result;
        switch (currency) {
            case "XCH":
                result = HttpClientUtil.socks("127.0.0.1", 1080)
                        .doGet((https ? "https://" : "http://") + "api-aws.huobi.pro/market/detail/merged?symbol=xchusdt");
                break;
            case "USD":
                result = HttpClientUtil.http("127.0.0.1", 1090)
                        .doGet((https ? "https://" : "http://") + "open.er-api.com/v6/latest/USD");
                break;
            default:
                return ApiResult.fail(ErrorCode.ASSET_TYPE_NOT_EXIST);
        }
        return ApiResult.success(JsonUtil.readValue(result, Map.class));
    }

    @ApiOperation("会员列表")
    @GetMapping("member/list")
    public ApiResult<PageInfo<Member>> listMember(@Valid @ModelAttribute PageRequest request) {
        return ApiResult.success(memberService.listMember(request));
    }

    @ApiOperation("查找会员")
    @GetMapping("member/search")
    public ApiResult<Member> searchMember(
            @ApiParam(value = "账户", required = true)
            @NotBlank @RequestParam String account) {
        return ApiResult.success(memberService.searchMember(account));
    }

    @ApiOperation("创建会员")
    @PostMapping("member/create")
    public ApiResult<Long> createMember(
            @ApiParam(value = "账户", required = true)
            @NotBlank @RequestParam String account,
            @ApiParam(value = "密码", required = true)
            @NotBlank @RequestParam String password,
            @ApiParam(value = "回滚", required = true)
            @RequestParam(defaultValue = "false") boolean rollback) {
        return ApiResult.success(memberService.createMember(account, password, rollback));
    }

    @ApiOperation("资产操作")
    @PostMapping("asset/{businessType}")
    public ApiResult<AccountAsset> asset(
            @ApiParam(value = "资产类型", required = true)
            @NotNull @RequestParam AssetTypeEnum assetType,
            @ApiParam(value = "操作数额", required = true)
            @NotNull @Min(0) @RequestParam BigDecimal amount,
            @ApiParam(value = "业务类型", required = true)
            @NotNull @PathVariable AssetBusinessTypeEnum businessType) {
        AccountAsset asset = null;

        switch (businessType) {
            case ADD:
                asset = accountAssetService.increase(100000L, assetType.getType(), amount, businessType);
                break;
            case COST:
                asset = accountAssetService.cost(100000L, assetType.getType(), amount, businessType);
                break;
            case FROZEN:
                asset = accountAssetService.frozen(100000L, assetType.getType(), amount, businessType);
                break;
            case UNFROZEN:
                asset = accountAssetService.unFrozen(100000L, assetType.getType(), amount, businessType);
                break;
            case COST_FROZEN:
                asset = accountAssetService.costFrozen(100000L, assetType.getType(), amount, businessType);
                break;
            default:
        }

        return ApiResult.success(asset);
    }
}
